/*
 * @Descripttion: 
 * @version: 
 * @Author: wangzhilei
 * @Date: 2021-09-04 01:18:31 
 * @LastEditors: wangzhilei
 * @LastEditTime: 2021-09-04 02:42:51
 */
#define TCP_PORT 8888
static int init_tcp_proxy_node(DB_DATA_PT pdata)
{
    if ((pdata->sock_fd == socket(AF_INET, SOCK_STRAM, 0)) < 0)
    {
        perror("socker create error!\n");
    }
    struct sockaddr_in servaddr;
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(TCP_PORT);
    if (inet_pton(AF_INET, TCP_SERVER, &servaddr.sin_addr) <= 0)
        perror("inet_pton error\n");
    if (connect(pdata->sock_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        perror("connect error\n");
}
static int tcp_proxy_send(DB_DATA_PT pdata, void *sendbuffer)
{
    int ret = write(pdata->sock_fd, sendbuffer, sizeof(sendbuffer));
    if (ret < 0)
        perror("write error\n");
    else
        printf("send success\n");
    return ret;
}
static DB_DATA_T tcp_proxy_node = {
    .id = TCP,
    .flag = INUSE,
    .Init = init_tcp_proxy_node,
    .SendFunction = tcp_proxy_send,
};
static void register_tcp_proxy_method(DB_LIST_PT ptlist, void *pdata)
{
    if (ptlist == NULL || pdata == NULL)
    {
        errno = EINVAL;
        return;
    }
    core_list_insert_before(&ptlist, 1, pdata);
}
void init_proxy_tcp(DB_LIST_PT ptlist)
{
    register_tcp_proxy_method(ptlist, (void *)&init_tcp_proxy_node);
}